home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / MacGnuGo 0.5e / gnugo.src / findnext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-22  |  2.8 KB  |  157 lines  |  [TEXT/R*ch]

  1. #include "comment.header"
  2.  
  3. #define EMPTY 0
  4.  
  5. extern unsigned char p[19][19], ma[19][19];
  6. extern int currentStone, MAXX, MAXY;
  7. extern int lib;
  8. extern countlib(int,int,int);
  9. extern int getcsize(int,int);
  10.  
  11. int fval(int newlib, int minlib, int csize)
  12.      /* evaluate new move */
  13. {
  14.   int k, val;
  15.   
  16.   if (newlib <= minlib)
  17.     val = -1;
  18.   else
  19.     {
  20.       k = newlib - minlib;
  21.       /* val = 40 + (k - 1) * 50 / (minlib * minlib * minlib);*/
  22.       val = 20 + csize * (20 + (k - 1) * 50 / (minlib * minlib));
  23.     }
  24.   return (val);
  25. }  /* end fval */
  26.  
  27.  
  28. int findnextmove(int m, int n, int *i, int *j, int *val, int minlib)
  29.      /* find new move i, j from group containing m, n */
  30. {
  31.   int ti, tj, tval;
  32.   int found = 0;
  33.   int csize = getcsize(m,n);
  34.   
  35.   *i = -1;   *j = -1;    *val = -1;
  36.   /* mark current position */
  37.   ma[m][n] = 1;
  38.   
  39.   /* check North neighbor */
  40.   if (m != 0)
  41.     if (p[m - 1][n] == EMPTY)
  42.       {
  43.     ti = m - 1;
  44.     tj = n;
  45.     lib = 0;
  46.     countlib(ti, tj, currentStone);
  47.     tval = fval(lib, minlib, csize);
  48.     found = 1;
  49.       }
  50.     else
  51.       if ((p[m - 1][n] == currentStone) && !ma[m - 1][n])
  52.     if (findnextmove(m - 1, n, &ti, &tj, &tval, minlib))
  53.       found = 1;
  54.   
  55.   if (found)
  56.     {
  57.       found = 0;
  58.       if (tval > *val)
  59.     {
  60.       *val = tval;
  61.       *i = ti;
  62.       *j = tj;
  63.     }
  64.       if (minlib == 1) return 1;
  65.     }
  66.   
  67.   /* check South neighbor */
  68.   if (m != MAXY - 1)
  69.     if (p[m + 1][n] == EMPTY)
  70.       {
  71.     ti = m + 1;
  72.     tj = n;
  73.     lib = 0;
  74.     countlib(ti, tj, currentStone);
  75.     tval = fval(lib, minlib, csize);
  76.     found = 1;
  77.       }
  78.     else
  79.       if ((p[m + 1][n] == currentStone) && !ma[m + 1][n])
  80.     if (findnextmove(m + 1, n, &ti, &tj, &tval, minlib))
  81.       found = 1;
  82.   
  83.   if (found)
  84.     {
  85.       found = 0;
  86.       if (tval > *val)
  87.     {
  88.       *val = tval;
  89.       *i = ti;
  90.       *j = tj;
  91.     }
  92.       if (minlib == 1) return 1;
  93.     }
  94.   
  95.   /* check West neighbor */
  96.   if (n != 0)
  97.     if (p[m][n - 1] == EMPTY)
  98.       {
  99.     ti = m;
  100.     tj = n - 1;
  101.     lib = 0;
  102.     countlib(ti, tj, currentStone);
  103.     tval = fval(lib, minlib, csize);
  104.     found = 1;
  105.       }
  106.     else
  107.       if ((p[m][n - 1] == currentStone) && !ma[m][n - 1])
  108.     if (findnextmove(m, n - 1, &ti, &tj, &tval, minlib))
  109.       found = 1;
  110.   
  111.   if (found)
  112.     {
  113.       found = 0;
  114.       if (tval > *val)
  115.     {
  116.       *val = tval;
  117.       *i = ti;
  118.       *j = tj;
  119.     }
  120.       if (minlib == 1) return 1;
  121.     }
  122.   
  123.   /* check East neighbor */
  124.   if (n != MAXX - 1)
  125.     if (p[m][n + 1] == EMPTY)
  126.       {
  127.     ti = m;
  128.     tj = n + 1;
  129.     lib = 0;
  130.     countlib(ti, tj, currentStone);
  131.     tval = fval(lib, minlib, csize);
  132.     found = 1;
  133.       }
  134.     else
  135.       if ((p[m][n + 1] == currentStone) && !ma[m][n + 1])
  136.     if (findnextmove(m, n + 1, &ti, &tj, &tval, minlib))
  137.       found = 1;
  138.   
  139.   if (found)
  140.     {
  141.       found = 0;
  142.       if (tval > *val)
  143.     {
  144.       *val = tval;
  145.       *i = ti;
  146.       *j = tj;
  147.     }
  148.       if (minlib == 1) return 1;
  149.     }
  150.   
  151.   if (*val > 0)    /* found next move */
  152.     return 1;
  153.   else    /* next move failed */
  154.     return 0;
  155. }  /* end findnextmove */
  156.  
  157.